package ds;

public class MyTree {
    public static class Node {
        public SkillTree.Skill val;
        public Node left, right;
        public Node(SkillTree.Skill v) { val = v; }
    }
    private Node root;
    public void insert(SkillTree.Skill val) {
        root = insert(root, val);
    }
    private Node insert(Node node, SkillTree.Skill val) {
        if (node == null) return new Node(val);
        if (val.compareTo(node.val) < 0) node.left = insert(node.left, val);
        else node.right = insert(node.right, val);
        return node;
    }
    public Node getRoot() { return root; }
    
    
    // 先序遍历，返回所有节点的列表
    public java.util.List<SkillTree.Skill> traverse() {
        java.util.List<SkillTree.Skill> result = new java.util.ArrayList<>();
        traverse(root, result);
        return result;
    }
    private void traverse(Node node, java.util.List<SkillTree.Skill> result) {
        if (node == null) return;
        result.add(node.val);
        traverse(node.left, result);
        traverse(node.right, result);
    }
    // 计算二叉树深度
    public int getDepth() {
        return getDepth(root);
    }
    private int getDepth(Node node) {
        if (node == null) return 0;
        return 1 + Math.max(getDepth(node.left), getDepth(node.right));
    }
    // 统计节点个数
    public int getNodeCount() {
        return getNodeCount(root);
    }
    private int getNodeCount(Node node) {
        if (node == null) return 0;
        return 1 + getNodeCount(node.left) + getNodeCount(node.right);
    }
    // 统计叶子节点个数
    public int getLeafCount() {
        return getLeafCount(root);
    }
    private int getLeafCount(Node node) {
        if (node == null) return 0;
        if (node.left == null && node.right == null) return 1;
        return getLeafCount(node.left) + getLeafCount(node.right);
    }
} 